package com.alander.nexusgo.post.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alander.nexusgo.common.model.dto.PostLikesListDTO;
import com.alander.nexusgo.common.model.vo.PostLikesVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.alander.nexusgo.post.model.entity.PostLikes;
import com.alander.nexusgo.post.service.PostLikesService;
import com.alander.nexusgo.post.service.PostsService;
import com.alander.nexusgo.post.service.PostCommentsService;
import com.alander.nexusgo.post.mapper.PostLikesMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author alander
*/
@Service
public class PostLikesServiceImpl extends ServiceImpl<PostLikesMapper, PostLikes>
    implements PostLikesService{
    
    @Autowired
    private PostsService postsService;
    
    @Autowired
    private PostCommentsService postCommentsService;
    
    @Override
    public String like(Long targetId, Integer targetType, Long userId) {
        // 检查是否已点赞
        LambdaQueryWrapper<PostLikes> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PostLikes::getUserId, userId)
                .eq(PostLikes::getTargetId, targetId)
                .eq(PostLikes::getTargetType, targetType);
        
        PostLikes existingLike = this.getOne(queryWrapper);
        
        if (existingLike != null) {
            // 取消点赞
            LambdaUpdateWrapper<PostLikes> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(PostLikes::getUserId, userId)
                    .eq(PostLikes::getTargetId, targetId)
                    .eq(PostLikes::getTargetType, targetType)
                    .set(PostLikes::getDelFlag, "1");
            this.update(updateWrapper);
            
            // 更新点赞数
            if (targetType == 1) {
                // 帖子点赞数减1
                postsService.update()
                    .eq("id", targetId)
                    .setSql("like_count = like_count - 1")
                    .update();
            } else if (targetType == 2) {
                // 评论点赞数减1
                postCommentsService.update()
                    .eq("id", targetId)
                    .setSql("like_count = like_count - 1")
                    .update();
            }
            
            return "已取消点赞";
        } else {
            // 添加点赞
            PostLikes like = new PostLikes();
            like.setTargetId(targetId);
            like.setTargetType(targetType);
            like.setUserId(userId);
            this.save(like);
            
            // 更新点赞数
            if (targetType == 1) {
                // 帖子点赞数加1
                postsService.update()
                    .eq("id", targetId)
                    .setSql("like_count = like_count + 1")
                    .update();
            } else if (targetType == 2) {
                // 评论点赞数加1
                postCommentsService.update()
                    .eq("id", targetId)
                    .setSql("like_count = like_count + 1")
                    .update();
            }
            
            return "点赞成功";
        }
    }
    
    @Override
    public boolean checkLike(Long targetId, Integer targetType, Long userId) {
        LambdaQueryWrapper<PostLikes> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PostLikes::getUserId, userId)
                .eq(PostLikes::getTargetId, targetId)
                .eq(PostLikes::getTargetType, targetType);
        
        return this.getOne(queryWrapper) != null;
    }
    
    @Override
    public PostLikesVO getLikeById(Long id) {
        PostLikes like = this.getById(id);
        return like != null ? convertToVO(like) : null;
    }
    
    @Override
    public List<PostLikesVO> listLikes(PostLikesListDTO postLikesListDTO) {
        LambdaQueryWrapper<PostLikes> queryWrapper = new LambdaQueryWrapper<>();
        
        if (postLikesListDTO.getTargetId() != null) {
            queryWrapper.eq(PostLikes::getTargetId, postLikesListDTO.getTargetId());
        }
        
        if (postLikesListDTO.getTargetType() != null) {
            queryWrapper.eq(PostLikes::getTargetType, postLikesListDTO.getTargetType());
        }
        
        if (postLikesListDTO.getUserId() != null) {
            queryWrapper.eq(PostLikes::getUserId, postLikesListDTO.getUserId());
        }
        
        // 只查询未删除的点赞
        queryWrapper.eq(PostLikes::getDelFlag, "0");
        
        queryWrapper.orderByDesc(PostLikes::getCreateTime);
        List<PostLikes> likesList = this.list(queryWrapper);
        
        return likesList.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }
    
    /**
     * 将PostLikes实体转换为PostLikesVO
     * @param like PostLikes实体
     * @return PostLikesVO对象
     */
    private PostLikesVO convertToVO(PostLikes like) {
        return BeanUtil.copyProperties(like, PostLikesVO.class);
    }
}